/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package demo.server;

import demo.handler.hello;
import demo.handler.DemoServlet;
import demo.handler.welcome;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.handler.HandlerList;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.servlet.ServletHolder;

/**
 *
 * @author root
 */
public class DemoServer {

    public static void main(String[] args) throws Exception {
        Server server = new Server();

        SelectChannelConnector connector = new SelectChannelConnector();
        connector.setPort(8888);
        server.addConnector(connector);

//        ResourceHandler resource_handler0 = new ResourceHandler();
//        resource_handler0.setDirectoriesListed(false);
//        resource_handler0.setWelcomeFiles(new String[]{ "hello.html" });
//        resource_handler0.setResourceBase("src/view");
//        
//        ResourceHandler resource_handler1 = new ResourceHandler();
//        resource_handler1.setDirectoriesListed(false);
//        resource_handler1.setWelcomeFiles(new String[]{ "welcome.html" });
//        resource_handler1.setResourceBase("src/view");
// 
//        HandlerList handlers0 = new HandlerList();
//        handlers0.setHandlers(new Handler[] { resource_handler0, resource_handler1 ,new DefaultHandler() });
//        
//        HandlerList handlers1 = new HandlerList();
//        handlers1.setHandlers(new Handler[] { resource_handler1, new DefaultHandler() });
//        server.setHandler(handlers);
        ContextHandler context0 = new ContextHandler();
        context0.setContextPath("/hello");
        context0.setResourceBase(".");
        context0.setClassLoader(Thread.currentThread().getContextClassLoader());
        context0.setHandler(new hello());

        ContextHandler context1 = new ContextHandler();
        context1.setContextPath("/welcome");
        context1.setResourceBase(".");
        context1.setClassLoader(Thread.currentThread().getContextClassLoader());
        context1.setHandler(new welcome());

//        ContextHandler context2 = new ContextHandler();
//        context2.setContextPath("/servlet");
//        context1.setResourceBase(".");
//        context1.setClassLoader(Thread.currentThread().getContextClassLoader());
//        context1.setHandler(new servlet());
//        ServletContextHandler context2 = new ServletContextHandler(ServletContextHandler.SESSIONS);
//        context2.setContextPath("/servlet");
          ServletHandler context2 = new ServletHandler();
          context2.addServletWithMapping("demo.handler.DemoServlet", "/index");
//        context2.setBaseResource(".");
//        server.setHandler(context);
// 
//        ServletHolder holder = context.addServlet(org.eclipse.jetty.servlet.DefaultServlet.class,"/tmp/*");
//        holder.setInitParameter("resourceBase","/tmp");
//        holder.setInitParameter("pathInfoOnly","true");

        HandlerList handlers = new HandlerList();
        handlers.setHandlers(new Handler[]{context0, context1, context2});
//        
//        server.setHandler(handlers);
//        context2.addServlet(new ServletHolder(new DemoServlet()), "/*");
//        context2.addServlet(new ServletHolder(new DemoServlet("Buongiorno Mondo")), "/it/*");
//        context2.addServlet(new ServletHolder(new DemoServlet("Bonjour le Monde")), "/fr/*");
//        server.setHandler(new HelloHandler());
        server.setHandler(handlers);
        server.start();
        server.join();
    }
}
